package modules.majiang.data
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * 倒计时的状态
	 * @author xumin.xu
	 */
	public class CountDownState
	{
		public function CountDownState()
		{
			timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
		}

		private function onTimer(event:Event):void
		{
			var currentTime:Number = new Date().time;
			if (currentTime > _startTime + _totalTime)
			{
				stopCountDown();
			}
			else
			{
				if (stepFunc != null)
				{
					//计算过去了的时间
					stepFunc(countDownView, (currentTime - _startTime) / _totalTime);
				}
			}
		}

		/** 开始时间 */
		private var _startTime:Number;

		public function get startTime():Number
		{
			return _startTime;
		}

		public function set startTime(value:Number):void
		{
			this._startTime = value;
			//开始倒计时
			if (startFunc != null)
			{
				startFunc();
			}
			timer.start();
		}

		/** 播放时长 */
		private var _totalTime:Number;

		public function get totalTime():Number
		{
			return _totalTime;
		}

		public function set totalTime(value:Number):void
		{
			this._totalTime = value;
		}

		private var timer:Timer;

		/**
		 * 调用此方法中止倒计时
		 **/
		public function stopCountDown():void
		{
			if (endFunc != null)
			{
				endFunc(countDownView);
			}
			//结束倒计时
			timer.stop();
		}

		/** 开始倒计时函数 */
		public var startFunc:Function;

		/** 每步函数，刷新函数 */
		public var stepFunc:Function;

		/** 结束倒计时函数 */
		public var endFunc:Function;

		public var countDownView:MovieClip;
	}
}
